package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import org.springframework.web.bind.annotation.RequestBody;


@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping("/findRoleByUserId")
    public Result findRoleByUserId(int id) {
        try {
            List<Integer> list = roleService.findByUserId(id);
            return new Result(true, "查询用户角色列表成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询用户角色列表失败");
        }
    }


    @RequestMapping("/deleteByRoleId")
    public Result deleteByRoleId(Integer RoleId) {
        try {
            Result result = roleService.deleteByRoleId(RoleId);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> roles = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findPage(queryPageBean);

    }

    @RequestMapping("/findPermissionIdByRoleId")
    public Result findPermissionByRoleId(Integer roleId) {
        try {
            List<Integer> permissionIds = roleService.findPermissionIdByRoleId(roleId);
            return new Result(true, "查询角色权限成功", permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色权限失败");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds) {
        try {
            roleService.edit(role, permissionIds);
            return new Result(true, "修改角色权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改角色权限失败");
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds) {
        try {
            Result result = roleService.add(role, permissionIds);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    @RequestMapping("/findRoleByRoleId")
    public Result findRoleByRoleId(Integer roleId) {
        try {
            Role role = roleService.findRoleByRoleId(roleId);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }

    }
}
